{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Datawhale 零基础入门数据挖掘-Baseline\n",
    "\n",
    "## Baseline-v1.0 版\n",
    "\n",
    "Tip:这是一个最初始baseline版本,抛砖引玉,为大家提供一个基本Baseline和一个竞赛流程的基本介绍，欢迎大家多多交流。\n",
    "\n",
    "**赛题：零基础入门数据挖掘 - 二手车交易价格预测**\n",
    "\n",
    "地址：https://tianchi.aliyun.com/competition/entrance/231784/introduction?spm=5176.12281957.1004.1.38b02448ausjSX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "231784\n"
     ]
    }
   ],
   "source": [
    "# 查看数据文件目录  list datalab files\n",
    "!ls datalab/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1:导入函数工具箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 基础工具\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy.special import jn\n",
    "from IPython.display import display, clear_output\n",
    "import time\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline\n",
    "\n",
    "## 模型预测的\n",
    "from sklearn import linear_model\n",
    "from sklearn import preprocessing\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor\n",
    "\n",
    "## 数据降维处理的\n",
    "from sklearn.decomposition import PCA,FastICA,FactorAnalysis,SparsePCA\n",
    "\n",
    "import lightgbm as lgb\n",
    "import xgboost as xgb\n",
    "\n",
    "## 参数搜索和评价的\n",
    "from sklearn.model_selection import GridSearchCV,cross_val_score,StratifiedKFold,train_test_split\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2:数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train data shape: (150000, 31)\n",
      "TestA data shape: (50000, 30)\n"
     ]
    }
   ],
   "source": [
    "## 通过Pandas对于数据进行读取 (pandas是一个很友好的数据读取函数库)\n",
    "Train_data = pd.read_csv('datalab/231784/used_car_train_20200313.csv', sep=' ')\n",
    "TestA_data = pd.read_csv('datalab/231784/used_car_testA_20200313.csv', sep=' ')\n",
    "\n",
    "## 输出数据的大小信息\n",
    "print('Train data shape:',Train_data.shape)\n",
    "print('TestA data shape:',TestA_data.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1) 数据简要浏览"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID</th>\n",
       "      <th>name</th>\n",
       "      <th>regDate</th>\n",
       "      <th>model</th>\n",
       "      <th>brand</th>\n",
       "      <th>bodyType</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>gearbox</th>\n",
       "      <th>power</th>\n",
       "      <th>kilometer</th>\n",
       "      <th>...</th>\n",
       "      <th>v_5</th>\n",
       "      <th>v_6</th>\n",
       "      <th>v_7</th>\n",
       "      <th>v_8</th>\n",
       "      <th>v_9</th>\n",
       "      <th>v_10</th>\n",
       "      <th>v_11</th>\n",
       "      <th>v_12</th>\n",
       "      <th>v_13</th>\n",
       "      <th>v_14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>736</td>\n",
       "      <td>20040402</td>\n",
       "      <td>30.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>60</td>\n",
       "      <td>12.5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.235676</td>\n",
       "      <td>0.101988</td>\n",
       "      <td>0.129549</td>\n",
       "      <td>0.022816</td>\n",
       "      <td>0.097462</td>\n",
       "      <td>-2.881803</td>\n",
       "      <td>2.804097</td>\n",
       "      <td>-2.420821</td>\n",
       "      <td>0.795292</td>\n",
       "      <td>0.914762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2262</td>\n",
       "      <td>20030301</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.264777</td>\n",
       "      <td>0.121004</td>\n",
       "      <td>0.135731</td>\n",
       "      <td>0.026597</td>\n",
       "      <td>0.020582</td>\n",
       "      <td>-4.900482</td>\n",
       "      <td>2.096338</td>\n",
       "      <td>-1.030483</td>\n",
       "      <td>-1.722674</td>\n",
       "      <td>0.245522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>14874</td>\n",
       "      <td>20040403</td>\n",
       "      <td>115.0</td>\n",
       "      <td>15</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>163</td>\n",
       "      <td>12.5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.251410</td>\n",
       "      <td>0.114912</td>\n",
       "      <td>0.165147</td>\n",
       "      <td>0.062173</td>\n",
       "      <td>0.027075</td>\n",
       "      <td>-4.846749</td>\n",
       "      <td>1.803559</td>\n",
       "      <td>1.565330</td>\n",
       "      <td>-0.832687</td>\n",
       "      <td>-0.229963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>71865</td>\n",
       "      <td>19960908</td>\n",
       "      <td>109.0</td>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>193</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.274293</td>\n",
       "      <td>0.110300</td>\n",
       "      <td>0.121964</td>\n",
       "      <td>0.033395</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-4.509599</td>\n",
       "      <td>1.285940</td>\n",
       "      <td>-0.501868</td>\n",
       "      <td>-2.438353</td>\n",
       "      <td>-0.478699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>111080</td>\n",
       "      <td>20120103</td>\n",
       "      <td>110.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>68</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.228036</td>\n",
       "      <td>0.073205</td>\n",
       "      <td>0.091880</td>\n",
       "      <td>0.078819</td>\n",
       "      <td>0.121534</td>\n",
       "      <td>-1.896240</td>\n",
       "      <td>0.910783</td>\n",
       "      <td>0.931110</td>\n",
       "      <td>2.834518</td>\n",
       "      <td>1.923482</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   SaleID    name   regDate  model  brand  bodyType  fuelType  gearbox  power  \\\n",
       "0       0     736  20040402   30.0      6       1.0       0.0      0.0     60   \n",
       "1       1    2262  20030301   40.0      1       2.0       0.0      0.0      0   \n",
       "2       2   14874  20040403  115.0     15       1.0       0.0      0.0    163   \n",
       "3       3   71865  19960908  109.0     10       0.0       0.0      1.0    193   \n",
       "4       4  111080  20120103  110.0      5       1.0       0.0      0.0     68   \n",
       "\n",
       "   kilometer    ...          v_5       v_6       v_7       v_8       v_9  \\\n",
       "0       12.5    ...     0.235676  0.101988  0.129549  0.022816  0.097462   \n",
       "1       15.0    ...     0.264777  0.121004  0.135731  0.026597  0.020582   \n",
       "2       12.5    ...     0.251410  0.114912  0.165147  0.062173  0.027075   \n",
       "3       15.0    ...     0.274293  0.110300  0.121964  0.033395  0.000000   \n",
       "4        5.0    ...     0.228036  0.073205  0.091880  0.078819  0.121534   \n",
       "\n",
       "       v_10      v_11      v_12      v_13      v_14  \n",
       "0 -2.881803  2.804097 -2.420821  0.795292  0.914762  \n",
       "1 -4.900482  2.096338 -1.030483 -1.722674  0.245522  \n",
       "2 -4.846749  1.803559  1.565330 -0.832687 -0.229963  \n",
       "3 -4.509599  1.285940 -0.501868 -2.438353 -0.478699  \n",
       "4 -1.896240  0.910783  0.931110  2.834518  1.923482  \n",
       "\n",
       "[5 rows x 31 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 通过.head() 简要浏览读取数据的形式\n",
    "Train_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2) 数据信息查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 150000 entries, 0 to 149999\n",
      "Data columns (total 31 columns):\n",
      "SaleID               150000 non-null int64\n",
      "name                 150000 non-null int64\n",
      "regDate              150000 non-null int64\n",
      "model                149999 non-null float64\n",
      "brand                150000 non-null int64\n",
      "bodyType             145494 non-null float64\n",
      "fuelType             141320 non-null float64\n",
      "gearbox              144019 non-null float64\n",
      "power                150000 non-null int64\n",
      "kilometer            150000 non-null float64\n",
      "notRepairedDamage    150000 non-null object\n",
      "regionCode           150000 non-null int64\n",
      "seller               150000 non-null int64\n",
      "offerType            150000 non-null int64\n",
      "creatDate            150000 non-null int64\n",
      "price                150000 non-null int64\n",
      "v_0                  150000 non-null float64\n",
      "v_1                  150000 non-null float64\n",
      "v_2                  150000 non-null float64\n",
      "v_3                  150000 non-null float64\n",
      "v_4                  150000 non-null float64\n",
      "v_5                  150000 non-null float64\n",
      "v_6                  150000 non-null float64\n",
      "v_7                  150000 non-null float64\n",
      "v_8                  150000 non-null float64\n",
      "v_9                  150000 non-null float64\n",
      "v_10                 150000 non-null float64\n",
      "v_11                 150000 non-null float64\n",
      "v_12                 150000 non-null float64\n",
      "v_13                 150000 non-null float64\n",
      "v_14                 150000 non-null float64\n",
      "dtypes: float64(20), int64(10), object(1)\n",
      "memory usage: 35.5+ MB\n"
     ]
    }
   ],
   "source": [
    "## 通过 .info() 简要可以看到对应一些数据列名，以及NAN缺失信息\n",
    "Train_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['SaleID', 'name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType',\n",
       "       'gearbox', 'power', 'kilometer', 'notRepairedDamage', 'regionCode',\n",
       "       'seller', 'offerType', 'creatDate', 'price', 'v_0', 'v_1', 'v_2', 'v_3',\n",
       "       'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12',\n",
       "       'v_13', 'v_14'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 通过 .columns 查看列名\n",
    "Train_data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 50000 entries, 0 to 49999\n",
      "Data columns (total 30 columns):\n",
      "SaleID               50000 non-null int64\n",
      "name                 50000 non-null int64\n",
      "regDate              50000 non-null int64\n",
      "model                50000 non-null float64\n",
      "brand                50000 non-null int64\n",
      "bodyType             48587 non-null float64\n",
      "fuelType             47107 non-null float64\n",
      "gearbox              48090 non-null float64\n",
      "power                50000 non-null int64\n",
      "kilometer            50000 non-null float64\n",
      "notRepairedDamage    50000 non-null object\n",
      "regionCode           50000 non-null int64\n",
      "seller               50000 non-null int64\n",
      "offerType            50000 non-null int64\n",
      "creatDate            50000 non-null int64\n",
      "v_0                  50000 non-null float64\n",
      "v_1                  50000 non-null float64\n",
      "v_2                  50000 non-null float64\n",
      "v_3                  50000 non-null float64\n",
      "v_4                  50000 non-null float64\n",
      "v_5                  50000 non-null float64\n",
      "v_6                  50000 non-null float64\n",
      "v_7                  50000 non-null float64\n",
      "v_8                  50000 non-null float64\n",
      "v_9                  50000 non-null float64\n",
      "v_10                 50000 non-null float64\n",
      "v_11                 50000 non-null float64\n",
      "v_12                 50000 non-null float64\n",
      "v_13                 50000 non-null float64\n",
      "v_14                 50000 non-null float64\n",
      "dtypes: float64(20), int64(9), object(1)\n",
      "memory usage: 11.4+ MB\n"
     ]
    }
   ],
   "source": [
    "TestA_data.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3) 数据统计信息浏览"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID</th>\n",
       "      <th>name</th>\n",
       "      <th>regDate</th>\n",
       "      <th>model</th>\n",
       "      <th>brand</th>\n",
       "      <th>bodyType</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>gearbox</th>\n",
       "      <th>power</th>\n",
       "      <th>kilometer</th>\n",
       "      <th>...</th>\n",
       "      <th>v_5</th>\n",
       "      <th>v_6</th>\n",
       "      <th>v_7</th>\n",
       "      <th>v_8</th>\n",
       "      <th>v_9</th>\n",
       "      <th>v_10</th>\n",
       "      <th>v_11</th>\n",
       "      <th>v_12</th>\n",
       "      <th>v_13</th>\n",
       "      <th>v_14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>1.500000e+05</td>\n",
       "      <td>149999.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>145494.000000</td>\n",
       "      <td>141320.000000</td>\n",
       "      <td>144019.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>74999.500000</td>\n",
       "      <td>68349.172873</td>\n",
       "      <td>2.003417e+07</td>\n",
       "      <td>47.129021</td>\n",
       "      <td>8.052733</td>\n",
       "      <td>1.792369</td>\n",
       "      <td>0.375842</td>\n",
       "      <td>0.224943</td>\n",
       "      <td>119.316547</td>\n",
       "      <td>12.597160</td>\n",
       "      <td>...</td>\n",
       "      <td>0.248204</td>\n",
       "      <td>0.044923</td>\n",
       "      <td>0.124692</td>\n",
       "      <td>0.058144</td>\n",
       "      <td>0.061996</td>\n",
       "      <td>-0.001000</td>\n",
       "      <td>0.009035</td>\n",
       "      <td>0.004813</td>\n",
       "      <td>0.000313</td>\n",
       "      <td>-0.000688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>43301.414527</td>\n",
       "      <td>61103.875095</td>\n",
       "      <td>5.364988e+04</td>\n",
       "      <td>49.536040</td>\n",
       "      <td>7.864956</td>\n",
       "      <td>1.760640</td>\n",
       "      <td>0.548677</td>\n",
       "      <td>0.417546</td>\n",
       "      <td>177.168419</td>\n",
       "      <td>3.919576</td>\n",
       "      <td>...</td>\n",
       "      <td>0.045804</td>\n",
       "      <td>0.051743</td>\n",
       "      <td>0.201410</td>\n",
       "      <td>0.029186</td>\n",
       "      <td>0.035692</td>\n",
       "      <td>3.772386</td>\n",
       "      <td>3.286071</td>\n",
       "      <td>2.517478</td>\n",
       "      <td>1.288988</td>\n",
       "      <td>1.038685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.991000e+07</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-9.168192</td>\n",
       "      <td>-5.558207</td>\n",
       "      <td>-9.639552</td>\n",
       "      <td>-4.153899</td>\n",
       "      <td>-6.546556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>37499.750000</td>\n",
       "      <td>11156.000000</td>\n",
       "      <td>1.999091e+07</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>12.500000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.243615</td>\n",
       "      <td>0.000038</td>\n",
       "      <td>0.062474</td>\n",
       "      <td>0.035334</td>\n",
       "      <td>0.033930</td>\n",
       "      <td>-3.722303</td>\n",
       "      <td>-1.951543</td>\n",
       "      <td>-1.871846</td>\n",
       "      <td>-1.057789</td>\n",
       "      <td>-0.437034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>74999.500000</td>\n",
       "      <td>51638.000000</td>\n",
       "      <td>2.003091e+07</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>110.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.257798</td>\n",
       "      <td>0.000812</td>\n",
       "      <td>0.095866</td>\n",
       "      <td>0.057014</td>\n",
       "      <td>0.058484</td>\n",
       "      <td>1.624076</td>\n",
       "      <td>-0.358053</td>\n",
       "      <td>-0.130753</td>\n",
       "      <td>-0.036245</td>\n",
       "      <td>0.141246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>112499.250000</td>\n",
       "      <td>118841.250000</td>\n",
       "      <td>2.007111e+07</td>\n",
       "      <td>66.000000</td>\n",
       "      <td>13.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>150.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.265297</td>\n",
       "      <td>0.102009</td>\n",
       "      <td>0.125243</td>\n",
       "      <td>0.079382</td>\n",
       "      <td>0.087491</td>\n",
       "      <td>2.844357</td>\n",
       "      <td>1.255022</td>\n",
       "      <td>1.776933</td>\n",
       "      <td>0.942813</td>\n",
       "      <td>0.680378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>149999.000000</td>\n",
       "      <td>196812.000000</td>\n",
       "      <td>2.015121e+07</td>\n",
       "      <td>247.000000</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>19312.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.291838</td>\n",
       "      <td>0.151420</td>\n",
       "      <td>1.404936</td>\n",
       "      <td>0.160791</td>\n",
       "      <td>0.222787</td>\n",
       "      <td>12.357011</td>\n",
       "      <td>18.819042</td>\n",
       "      <td>13.847792</td>\n",
       "      <td>11.147669</td>\n",
       "      <td>8.658418</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              SaleID           name       regDate          model  \\\n",
       "count  150000.000000  150000.000000  1.500000e+05  149999.000000   \n",
       "mean    74999.500000   68349.172873  2.003417e+07      47.129021   \n",
       "std     43301.414527   61103.875095  5.364988e+04      49.536040   \n",
       "min         0.000000       0.000000  1.991000e+07       0.000000   \n",
       "25%     37499.750000   11156.000000  1.999091e+07      10.000000   \n",
       "50%     74999.500000   51638.000000  2.003091e+07      30.000000   \n",
       "75%    112499.250000  118841.250000  2.007111e+07      66.000000   \n",
       "max    149999.000000  196812.000000  2.015121e+07     247.000000   \n",
       "\n",
       "               brand       bodyType       fuelType        gearbox  \\\n",
       "count  150000.000000  145494.000000  141320.000000  144019.000000   \n",
       "mean        8.052733       1.792369       0.375842       0.224943   \n",
       "std         7.864956       1.760640       0.548677       0.417546   \n",
       "min         0.000000       0.000000       0.000000       0.000000   \n",
       "25%         1.000000       0.000000       0.000000       0.000000   \n",
       "50%         6.000000       1.000000       0.000000       0.000000   \n",
       "75%        13.000000       3.000000       1.000000       0.000000   \n",
       "max        39.000000       7.000000       6.000000       1.000000   \n",
       "\n",
       "               power      kilometer      ...                  v_5  \\\n",
       "count  150000.000000  150000.000000      ...        150000.000000   \n",
       "mean      119.316547      12.597160      ...             0.248204   \n",
       "std       177.168419       3.919576      ...             0.045804   \n",
       "min         0.000000       0.500000      ...             0.000000   \n",
       "25%        75.000000      12.500000      ...             0.243615   \n",
       "50%       110.000000      15.000000      ...             0.257798   \n",
       "75%       150.000000      15.000000      ...             0.265297   \n",
       "max     19312.000000      15.000000      ...             0.291838   \n",
       "\n",
       "                 v_6            v_7            v_8            v_9  \\\n",
       "count  150000.000000  150000.000000  150000.000000  150000.000000   \n",
       "mean        0.044923       0.124692       0.058144       0.061996   \n",
       "std         0.051743       0.201410       0.029186       0.035692   \n",
       "min         0.000000       0.000000       0.000000       0.000000   \n",
       "25%         0.000038       0.062474       0.035334       0.033930   \n",
       "50%         0.000812       0.095866       0.057014       0.058484   \n",
       "75%         0.102009       0.125243       0.079382       0.087491   \n",
       "max         0.151420       1.404936       0.160791       0.222787   \n",
       "\n",
       "                v_10           v_11           v_12           v_13  \\\n",
       "count  150000.000000  150000.000000  150000.000000  150000.000000   \n",
       "mean       -0.001000       0.009035       0.004813       0.000313   \n",
       "std         3.772386       3.286071       2.517478       1.288988   \n",
       "min        -9.168192      -5.558207      -9.639552      -4.153899   \n",
       "25%        -3.722303      -1.951543      -1.871846      -1.057789   \n",
       "50%         1.624076      -0.358053      -0.130753      -0.036245   \n",
       "75%         2.844357       1.255022       1.776933       0.942813   \n",
       "max        12.357011      18.819042      13.847792      11.147669   \n",
       "\n",
       "                v_14  \n",
       "count  150000.000000  \n",
       "mean       -0.000688  \n",
       "std         1.038685  \n",
       "min        -6.546556  \n",
       "25%        -0.437034  \n",
       "50%         0.141246  \n",
       "75%         0.680378  \n",
       "max         8.658418  \n",
       "\n",
       "[8 rows x 30 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 通过 .describe() 可以查看数值特征列的一些统计信息\n",
    "Train_data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID</th>\n",
       "      <th>name</th>\n",
       "      <th>regDate</th>\n",
       "      <th>model</th>\n",
       "      <th>brand</th>\n",
       "      <th>bodyType</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>gearbox</th>\n",
       "      <th>power</th>\n",
       "      <th>kilometer</th>\n",
       "      <th>...</th>\n",
       "      <th>v_5</th>\n",
       "      <th>v_6</th>\n",
       "      <th>v_7</th>\n",
       "      <th>v_8</th>\n",
       "      <th>v_9</th>\n",
       "      <th>v_10</th>\n",
       "      <th>v_11</th>\n",
       "      <th>v_12</th>\n",
       "      <th>v_13</th>\n",
       "      <th>v_14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>5.000000e+04</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>48587.000000</td>\n",
       "      <td>47107.000000</td>\n",
       "      <td>48090.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>174999.500000</td>\n",
       "      <td>68542.223280</td>\n",
       "      <td>2.003393e+07</td>\n",
       "      <td>46.844520</td>\n",
       "      <td>8.056240</td>\n",
       "      <td>1.782185</td>\n",
       "      <td>0.373405</td>\n",
       "      <td>0.224350</td>\n",
       "      <td>119.883620</td>\n",
       "      <td>12.595580</td>\n",
       "      <td>...</td>\n",
       "      <td>0.248669</td>\n",
       "      <td>0.045021</td>\n",
       "      <td>0.122744</td>\n",
       "      <td>0.057997</td>\n",
       "      <td>0.062000</td>\n",
       "      <td>-0.017855</td>\n",
       "      <td>-0.013742</td>\n",
       "      <td>-0.013554</td>\n",
       "      <td>-0.003147</td>\n",
       "      <td>0.001516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>14433.901067</td>\n",
       "      <td>61052.808133</td>\n",
       "      <td>5.368870e+04</td>\n",
       "      <td>49.469548</td>\n",
       "      <td>7.819477</td>\n",
       "      <td>1.760736</td>\n",
       "      <td>0.546442</td>\n",
       "      <td>0.417158</td>\n",
       "      <td>185.097387</td>\n",
       "      <td>3.908979</td>\n",
       "      <td>...</td>\n",
       "      <td>0.044601</td>\n",
       "      <td>0.051766</td>\n",
       "      <td>0.195972</td>\n",
       "      <td>0.029211</td>\n",
       "      <td>0.035653</td>\n",
       "      <td>3.747985</td>\n",
       "      <td>3.231258</td>\n",
       "      <td>2.515962</td>\n",
       "      <td>1.286597</td>\n",
       "      <td>1.027360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.991000e+07</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-9.160049</td>\n",
       "      <td>-5.411964</td>\n",
       "      <td>-8.916949</td>\n",
       "      <td>-4.123333</td>\n",
       "      <td>-6.112667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>162499.750000</td>\n",
       "      <td>11203.500000</td>\n",
       "      <td>1.999091e+07</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>12.500000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.243762</td>\n",
       "      <td>0.000044</td>\n",
       "      <td>0.062644</td>\n",
       "      <td>0.035084</td>\n",
       "      <td>0.033714</td>\n",
       "      <td>-3.700121</td>\n",
       "      <td>-1.971325</td>\n",
       "      <td>-1.876703</td>\n",
       "      <td>-1.060428</td>\n",
       "      <td>-0.437920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>174999.500000</td>\n",
       "      <td>52248.500000</td>\n",
       "      <td>2.003091e+07</td>\n",
       "      <td>29.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>109.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.257877</td>\n",
       "      <td>0.000815</td>\n",
       "      <td>0.095828</td>\n",
       "      <td>0.057084</td>\n",
       "      <td>0.058764</td>\n",
       "      <td>1.613212</td>\n",
       "      <td>-0.355843</td>\n",
       "      <td>-0.142779</td>\n",
       "      <td>-0.035956</td>\n",
       "      <td>0.138799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>187499.250000</td>\n",
       "      <td>118856.500000</td>\n",
       "      <td>2.007110e+07</td>\n",
       "      <td>65.000000</td>\n",
       "      <td>13.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>150.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.265328</td>\n",
       "      <td>0.102025</td>\n",
       "      <td>0.125438</td>\n",
       "      <td>0.079077</td>\n",
       "      <td>0.087489</td>\n",
       "      <td>2.832708</td>\n",
       "      <td>1.262914</td>\n",
       "      <td>1.764335</td>\n",
       "      <td>0.941469</td>\n",
       "      <td>0.681163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>199999.000000</td>\n",
       "      <td>196805.000000</td>\n",
       "      <td>2.015121e+07</td>\n",
       "      <td>246.000000</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>20000.000000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.291618</td>\n",
       "      <td>0.153265</td>\n",
       "      <td>1.358813</td>\n",
       "      <td>0.156355</td>\n",
       "      <td>0.214775</td>\n",
       "      <td>12.338872</td>\n",
       "      <td>18.856218</td>\n",
       "      <td>12.950498</td>\n",
       "      <td>5.913273</td>\n",
       "      <td>2.624622</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 29 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              SaleID           name       regDate         model         brand  \\\n",
       "count   50000.000000   50000.000000  5.000000e+04  50000.000000  50000.000000   \n",
       "mean   174999.500000   68542.223280  2.003393e+07     46.844520      8.056240   \n",
       "std     14433.901067   61052.808133  5.368870e+04     49.469548      7.819477   \n",
       "min    150000.000000       0.000000  1.991000e+07      0.000000      0.000000   \n",
       "25%    162499.750000   11203.500000  1.999091e+07     10.000000      1.000000   \n",
       "50%    174999.500000   52248.500000  2.003091e+07     29.000000      6.000000   \n",
       "75%    187499.250000  118856.500000  2.007110e+07     65.000000     13.000000   \n",
       "max    199999.000000  196805.000000  2.015121e+07    246.000000     39.000000   \n",
       "\n",
       "           bodyType      fuelType       gearbox         power     kilometer  \\\n",
       "count  48587.000000  47107.000000  48090.000000  50000.000000  50000.000000   \n",
       "mean       1.782185      0.373405      0.224350    119.883620     12.595580   \n",
       "std        1.760736      0.546442      0.417158    185.097387      3.908979   \n",
       "min        0.000000      0.000000      0.000000      0.000000      0.500000   \n",
       "25%        0.000000      0.000000      0.000000     75.000000     12.500000   \n",
       "50%        1.000000      0.000000      0.000000    109.000000     15.000000   \n",
       "75%        3.000000      1.000000      0.000000    150.000000     15.000000   \n",
       "max        7.000000      6.000000      1.000000  20000.000000     15.000000   \n",
       "\n",
       "           ...                v_5           v_6           v_7           v_8  \\\n",
       "count      ...       50000.000000  50000.000000  50000.000000  50000.000000   \n",
       "mean       ...           0.248669      0.045021      0.122744      0.057997   \n",
       "std        ...           0.044601      0.051766      0.195972      0.029211   \n",
       "min        ...           0.000000      0.000000      0.000000      0.000000   \n",
       "25%        ...           0.243762      0.000044      0.062644      0.035084   \n",
       "50%        ...           0.257877      0.000815      0.095828      0.057084   \n",
       "75%        ...           0.265328      0.102025      0.125438      0.079077   \n",
       "max        ...           0.291618      0.153265      1.358813      0.156355   \n",
       "\n",
       "                v_9          v_10          v_11          v_12          v_13  \\\n",
       "count  50000.000000  50000.000000  50000.000000  50000.000000  50000.000000   \n",
       "mean       0.062000     -0.017855     -0.013742     -0.013554     -0.003147   \n",
       "std        0.035653      3.747985      3.231258      2.515962      1.286597   \n",
       "min        0.000000     -9.160049     -5.411964     -8.916949     -4.123333   \n",
       "25%        0.033714     -3.700121     -1.971325     -1.876703     -1.060428   \n",
       "50%        0.058764      1.613212     -0.355843     -0.142779     -0.035956   \n",
       "75%        0.087489      2.832708      1.262914      1.764335      0.941469   \n",
       "max        0.214775     12.338872     18.856218     12.950498      5.913273   \n",
       "\n",
       "               v_14  \n",
       "count  50000.000000  \n",
       "mean       0.001516  \n",
       "std        1.027360  \n",
       "min       -6.112667  \n",
       "25%       -0.437920  \n",
       "50%        0.138799  \n",
       "75%        0.681163  \n",
       "max        2.624622  \n",
       "\n",
       "[8 rows x 29 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TestA_data.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3:特征与标签构建"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1) 提取数值类型特征列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['SaleID', 'name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType',\n",
      "       'gearbox', 'power', 'kilometer', 'regionCode', 'seller', 'offerType',\n",
      "       'creatDate', 'price', 'v_0', 'v_1', 'v_2', 'v_3', 'v_4', 'v_5', 'v_6',\n",
      "       'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "numerical_cols = Train_data.select_dtypes(exclude = 'object').columns\n",
    "print(numerical_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['notRepairedDamage'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "categorical_cols = Train_data.select_dtypes(include = 'object').columns\n",
    "print(categorical_cols)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2) 构建训练和测试样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X train shape: (150000, 18)\n",
      "X test shape: (50000, 18)\n"
     ]
    }
   ],
   "source": [
    "## 选择特征列\n",
    "feature_cols = [col for col in numerical_cols if col not in ['SaleID','name','regDate','creatDate','price','model','brand','regionCode','seller']]\n",
    "feature_cols = [col for col in feature_cols if 'Type' not in col]\n",
    "\n",
    "## 提前特征列，标签列构造训练样本和测试样本\n",
    "X_data = Train_data[feature_cols]\n",
    "Y_data = Train_data['price']\n",
    "\n",
    "X_test  = TestA_data[feature_cols]\n",
    "\n",
    "print('X train shape:',X_data.shape)\n",
    "print('X test shape:',X_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 定义了一个统计函数，方便后续信息统计\n",
    "def Sta_inf(data):\n",
    "    print('_min',np.min(data))\n",
    "    print('_max:',np.max(data))\n",
    "    print('_mean',np.mean(data))\n",
    "    print('_ptp',np.ptp(data))\n",
    "    print('_std',np.std(data))\n",
    "    print('_var',np.var(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3) 统计标签的基本分布信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sta of label:\n",
      "_min 11\n",
      "_max: 99999\n",
      "_mean 5923.32733333\n",
      "_ptp 99988\n",
      "_std 7501.97346988\n",
      "_var 56279605.9427\n"
     ]
    }
   ],
   "source": [
    "print('Sta of label:')\n",
    "Sta_inf(Y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAD6CAYAAABOIFvoAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADLxJREFUeJzt3V+InfWdx/H3Jx1ZpdYhf6ZrsazTItkLwVx0KFK1knEN/omCd8XKCl5k6Y1Iwa7e9W5jb7YKIubOFaF7UUo1QdoYg1UxtBNK4tJStCULuajMrDTpLjaw5LsX8+RXM2Y74/NM+syf9wsO85zvc07O82M88845Z56YqkKSJIAtYx+AJGntMAqSpMYoSJIaoyBJaoyCJKkxCpKkxihIkhqjIElqjIIkqZkY+wA+rR07dtT09PTYhyFJ68rx48cXqmpqudutuyhMT08zNzc39mFI0rqS5D9XcjvfPpIkNUZBktQYBUlSYxQkSY1RkCQ1RkGS1BgFSVJjFCRJjVGQJDXr7ozmIaafODTaY5/af+9ojy1JK+UrBUlSYxQkSc2KopDkiiSvfOz6C0mOJXk5yUSSK5McTHIiyYtZ1Ht2+ZYrSfpLlo1CkquA48Cd3fVbgYmquhm4BtgDPAScrqpdwNbutkNmkqQRLBuFqvqoqm4CTnejD4Cnl9x/Fjjcbb8O7B44kySN4FN/plBV71XVz5M8AJwHfgpsB850NzkLbBs4u0iSfUnmkszNz89/2kOWJK1Qrw+ak9wPPArcV1X/CywAk93uye76kNlFqupAVc1U1czU1LL/4yBJUk+fOgpJrgUeB/ZW1R+78REWP1uAxbeDjg6cSZJG0OeVwsPAF4CfJHkrySPAS8B1SU4CH7L4g37ITJI0ghWf0VxVN3RfnwKeusRN9i65fm7ATJI0Ak9ekyQ1RkGS1BgFSVJjFCRJjVGQJDVGQZLUGAVJUmMUJEmNUZAkNUZBktQYBUl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y++AU2z8NT8JPFdV0yz+ENvJxl/zBUPWtarr3yxR2DBnTSe5Fngc2FtVf+TSaxsyW3Oq6sGquhX4Bovvwz4L7EmyBbidj62l52wtOg5c+OcMbmAxEBt9zZ8D/tRtnwPeYeOv+YLVfh73f25X1Ya/AH8DHAROAi/SnZ+xHi/APwPvA291l39aurZLrXels7HXt8zap4HXWHyl8CbwH8C/dPt6z9bqBXgO+AXwb5thzd33920WY/DvwN9ugjW/333t/Zxd7ee2J69JkprN8vaRJGkFjIIkqTEKkqTGKEiSGqMgSWqMgiSpMQqSpOb/ADjvpJyHNP99AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "## 绘制标签的统计图，查看标签分布\n",
    "plt.hist(Y_data)\n",
    "plt.show()\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4) 缺省值用-1填补"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_data = X_data.fillna(-1)\n",
    "X_test = X_test.fillna(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4:模型训练与预测"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1) 利用xgb进行五折交叉验证查看模型的参数效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train mae: 628.086664863\n",
      "Val mae 715.990013454\n"
     ]
    }
   ],
   "source": [
    "## xgb-Model\n",
    "xgr = xgb.XGBRegressor(n_estimators=120, learning_rate=0.1, gamma=0, subsample=0.8,\\\n",
    "        colsample_bytree=0.9, max_depth=7) #,objective ='reg:squarederror'\n",
    "\n",
    "scores_train = []\n",
    "scores = []\n",
    "\n",
    "## 5折交叉验证方式\n",
    "sk=StratifiedKFold(n_splits=5,shuffle=True,random_state=0)\n",
    "for train_ind,val_ind in sk.split(X_data,Y_data):\n",
    "    \n",
    "    train_x=X_data.iloc[train_ind].values\n",
    "    train_y=Y_data.iloc[train_ind]\n",
    "    val_x=X_data.iloc[val_ind].values\n",
    "    val_y=Y_data.iloc[val_ind]\n",
    "    \n",
    "    xgr.fit(train_x,train_y)\n",
    "    pred_train_xgb=xgr.predict(train_x)\n",
    "    pred_xgb=xgr.predict(val_x)\n",
    "    \n",
    "    score_train = mean_absolute_error(train_y,pred_train_xgb)\n",
    "    scores_train.append(score_train)\n",
    "    score = mean_absolute_error(val_y,pred_xgb)\n",
    "    scores.append(score)\n",
    "\n",
    "print('Train mae:',np.mean(score_train))\n",
    "print('Val mae',np.mean(scores))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2） 定义xgb和lgb模型函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_xgb(x_train,y_train):\n",
    "    model = xgb.XGBRegressor(n_estimators=150, learning_rate=0.1, gamma=0, subsample=0.8,\\\n",
    "        colsample_bytree=0.9, max_depth=7) #, objective ='reg:squarederror'\n",
    "    model.fit(x_train, y_train)\n",
    "    return model\n",
    "\n",
    "def build_model_lgb(x_train,y_train):\n",
    "    estimator = lgb.LGBMRegressor(num_leaves=127,n_estimators = 150)\n",
    "    param_grid = {\n",
    "        'learning_rate': [0.01, 0.05, 0.1, 0.2],\n",
    "    }\n",
    "    gbm = GridSearchCV(estimator, param_grid)\n",
    "    gbm.fit(x_train, y_train)\n",
    "    return gbm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3）切分数据集（Train,Val）进行模型训练，评价和预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Split data with val\n",
    "x_train,x_val,y_train,y_val = train_test_split(X_data,Y_data,test_size=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train lgb...\n",
      "MAE of val with lgb: 689.084070621\n",
      "Predict lgb...\n",
      "Sta of Predict lgb:\n",
      "_min -519.150259864\n",
      "_max: 88575.1087721\n",
      "_mean 5922.98242599\n",
      "_ptp 89094.259032\n",
      "_std 7377.29714126\n",
      "_var 54424513.1104\n"
     ]
    }
   ],
   "source": [
    "print('Train lgb...')\n",
    "model_lgb = build_model_lgb(x_train,y_train)\n",
    "val_lgb = model_lgb.predict(x_val)\n",
    "MAE_lgb = mean_absolute_error(y_val,val_lgb)\n",
    "print('MAE of val with lgb:',MAE_lgb)\n",
    "\n",
    "print('Predict lgb...')\n",
    "model_lgb_pre = build_model_lgb(X_data,Y_data)\n",
    "subA_lgb = model_lgb_pre.predict(X_test)\n",
    "print('Sta of Predict lgb:')\n",
    "Sta_inf(subA_lgb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train xgb...\n",
      "MAE of val with xgb: 715.37757816\n",
      "Predict xgb...\n",
      "Sta of Predict xgb:\n",
      "_min -165.479\n",
      "_max: 90051.8\n",
      "_mean 5922.9\n",
      "_ptp 90217.3\n",
      "_std 7361.13\n",
      "_var 5.41862e+07\n"
     ]
    }
   ],
   "source": [
    "print('Train xgb...')\n",
    "model_xgb = build_model_xgb(x_train,y_train)\n",
    "val_xgb = model_xgb.predict(x_val)\n",
    "MAE_xgb = mean_absolute_error(y_val,val_xgb)\n",
    "print('MAE of val with xgb:',MAE_xgb)\n",
    "\n",
    "print('Predict xgb...')\n",
    "model_xgb_pre = build_model_xgb(X_data,Y_data)\n",
    "subA_xgb = model_xgb_pre.predict(X_test)\n",
    "print('Sta of Predict xgb:')\n",
    "Sta_inf(subA_xgb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4）进行两模型的结果加权融合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MAE of val with Weighted ensemble: 687.275745703\n"
     ]
    }
   ],
   "source": [
    "## 这里我们采取了简单的加权融合的方式\n",
    "val_Weighted = (1-MAE_lgb/(MAE_xgb+MAE_lgb))*val_lgb+(1-MAE_xgb/(MAE_xgb+MAE_lgb))*val_xgb\n",
    "val_Weighted[val_Weighted<0]=10 # 由于我们发现预测的最小值有负数，而真实情况下，price为负是不存在的，由此我们进行对应的后修正\n",
    "print('MAE of val with Weighted ensemble:',mean_absolute_error(y_val,val_Weighted))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAD6CAYAAABOIFvoAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADLxJREFUeJzt3V+InfWdx/H3Jx1ZpdYhf6ZrsazTItkLwVx0KFK1knEN/omCd8XKCl5k6Y1Iwa7e9W5jb7YKIubOFaF7UUo1QdoYg1UxtBNK4tJStCULuajMrDTpLjaw5LsX8+RXM2Y74/NM+syf9wsO85zvc07O82M88845Z56YqkKSJIAtYx+AJGntMAqSpMYoSJIaoyBJaoyCJKkxCpKkxihIkhqjIElqjIIkqZkY+wA+rR07dtT09PTYhyFJ68rx48cXqmpqudutuyhMT08zNzc39mFI0rqS5D9XcjvfPpIkNUZBktQYBUlSYxQkSY1RkCQ1RkGS1BgFSVJjFCRJjVGQJDXr7ozmIaafODTaY5/af+9ojy1JK+UrBUlSYxQkSc2KopDkiiSvfOz6C0mOJXk5yUSSK5McT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wITVXUzcA3wCJ9c26XWu9LZmpTkeuDh7upjwCFgF3B3kp0DZ2tOki8DN3bf51eB77PB1wx8EzhWVbcANwLPs0HXnOQq4Dh/fs4Nec6u6nN7s0RhFjjcbV84a3q9+gB4utveAnyXT67tUutd6Wytehp4stueBQ5X1XngDT62lp6ztegOYGuSnwG3AV9i46/5D8DVST4DXAV8jQ265qr6qKpuAk53oyHP2VV9bm+WKCw9a3rbiMcySFW9V1U/T/IAcB74JZ9c26XWu9LZmpPkQeAE8KtuNGR962LNwBQwX1VfB74IfJWNv+YfAXcBvwV+zeKxbvQ1X7Da/033Xv9micKyZ02vJ0nuBx4F7gN+zyfXdqn1rnS2Fu1l8W/OPwC+wuKp/ht9zWeBC/+cy++AU2z8NT8JPFdV0yz+ENvJxl/zBUPWtarr3yxR2DBnTSe5Fngc2FtVf+TSaxsyW3Oq6sGquhX4Bovvwz4L7EmyBbidj62l52wtOg5c+OcMbmAxEBt9zZ8D/tRtnwPeYeOv+YLVfh73f25X1Ya/AH8DHAROAi/SnZ+xHi/APwPvA291l39aurZLrXels7HXt8zap4HXWHyl8CbwH8C/dPt6z9bqBXgO+AXwb5thzd33920WY/DvwN9ugjW/333t/Zxd7ee2J69JkprN8vaRJGkFjIIkqTEKkqTGKEiSGqMgSWqMgiSpMQqSpOb/ADjvpJyHNP99AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sub_Weighted = (1-MAE_lgb/(MAE_xgb+MAE_lgb))*subA_lgb+(1-MAE_xgb/(MAE_xgb+MAE_lgb))*subA_xgb\n",
    "\n",
    "## 查看预测值的统计进行\n",
    "plt.hist(Y_data)\n",
    "plt.show()\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5）输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub = pd.DataFrame()\n",
    "sub['SaleID'] = X_test.index\n",
    "sub['price'] = sub_Weighted\n",
    "sub.to_csv('./sub_Weighted.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>39533.727414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>386.081960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>7791.974571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>11835.211966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>585.420407</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   SaleID         price\n",
       "0       0  39533.727414\n",
       "1       1    386.081960\n",
       "2       2   7791.974571\n",
       "3       3  11835.211966\n",
       "4       4    585.420407"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Baseline END.**\n",
    "\n",
    "--- By: ML67 \n",
    "\n",
    "        Email: maolinw67@163.com\n",
    "        PS: 华中科技大学研究生, 长期混迹Tianchi等，希望和大家多多交流。\n",
    "        github: https://github.com/mlw67 （近期会做一些书籍推导和代码的整理）\n",
    "\n",
    "--- By: AI蜗牛车\n",
    "\n",
    "        PS：东南大学研究生，研究方向主要是时空序列预测和时间序列数据挖掘\n",
    "        公众号： AI蜗牛车\n",
    "        知乎： https://www.zhihu.com/people/seu-aigua-niu-che\n",
    "        github: https://github.com/chehongshu\n",
    "        \n",
    "--- By:  阿泽\n",
    " \n",
    "        PS：复旦大学计算机研究生\n",
    "        知乎：阿泽 https://www.zhihu.com/people/is-aze（主要面向初学者的知识整理）\n",
    "\n",
    "--- By: 小雨姑娘\n",
    "\n",
    "        PS：数据挖掘爱好者，多次获得比赛TOP名次。\n",
    "        知乎：小雨姑娘的机器学习笔记：https://zhuanlan.zhihu.com/mlbasic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**关于Datawhale：**\n",
    "\n",
    "> Datawhale是一个专注于数据科学与AI领域的开源组织，汇集了众多领域院校和知名企业的优秀学习者，聚合了一群有开源精神和探索精神的团队成员。Datawhale 以“for the learner，和学习者一起成长”为愿景，鼓励真实地展现自我、开放包容、互信互助、敢于试错和勇于担当。同时 Datawhale 用开源的理念去探索开源内容、开源学习和开源方案，赋能人才培养，助力人才成长，建立起人与人，人与知识，人与企业和人与未来的联结。\n",
    "\n",
    "本次数据挖掘路径学习，专题知识将在天池分享，详情可关注Datawhale：\n",
    "\n",
    "![](http://jupter-oss.oss-cn-hangzhou.aliyuncs.com/public/files/image/2326541042/1584426326920_9FOUExG2be.jpg)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
